package com.raos.example.mongo;

import com.mongodb.*;

/**
 * mongodb java客户端 CRUD
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/8/29 21:25
 */
public class MongoCRUDTest {

    public static void main(String[] args) {
        Mongo mongo = new Mongo("127.0.0.1", 27017);

        DB db = new DB(mongo, "raos-test");

        DBCollection collection = db.getCollection("my_member");

        // 类比写法，类似JDBC
        DBObject dbObject = new BasicDBObject();
        dbObject.put("name", "yufeng");
        dbObject.put("age", 18);
        dbObject.put("addr", "Chengdu");

        collection.insert(dbObject);

        DBCursor cursor = collection.find();

        for (DBObject object : cursor) {
            System.out.println(object);
        }

        // 其他简化操作 ORM 框架
        // Morphia, MongoDB界的一个ORM鼻祖
        // MongoTemplate ...
    }

}
